<?php

use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //清空表
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        DB::table('model_has_permissions')->truncate();
        DB::table('model_has_roles')->truncate();
        DB::table('role_has_permissions')->truncate();
        DB::table('users')->truncate();
        DB::table('roles')->truncate();
        DB::table('permissions')->truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        //用户
        $user = User::create([
            'username'  => 'admin',
            'telephone' => '15081814130',
            'nickname'  => '超级管理员',
            'email'     => 'aa@bb.com',
            'password'  => bcrypt('111111'),
            'uuid'      => \Faker\Provider\Uuid::uuid()
        ]);

        //角色
        $role = Role::create([
            'name'          => 'admin',
            'display_name'  => '超级管理员'
        ]);

        //权限
        $permissions = [
            [
                'name'          => 'system/manage',
                'display_name'  => '系统管理',
                'is_menu'       => 1,
                'icon'          => 'el-icon-s-help',
                'children'      => [
                    [
                        'name'          => 'system/users',
                        'display_name'  => '用户管理',
                        'is_menu'       => 1,
                        'children'      => [
                            ['name' => 'system/users/create', 'display_name' => '添加用户'],
                            ['name' => 'system/users/update', 'display_name' => '编辑用户'],
                            ['name' => 'system/users/destroy', 'display_name' => '删除用户'],
                            ['name' => 'system/users/role', 'display_name' => '分配角色'],
                            ['name' => 'system/users/permission', 'display_name' => '分配权限'],
                        ]
                    ],
                    [
                        'name'          => 'system/roles',
                        'display_name'  => '角色管理',
                        'is_menu'       => 1,
                        'children'      => [
                            ['name' => 'system/roles/create', 'display_name' => '添加角色'],
                            ['name' => 'system/roles/update', 'display_name' => '编辑角色'],
                            ['name' => 'system/roles/destroy', 'display_name' => '删除角色'],
                            ['name' => 'system/roles/permission', 'display_name' => '分配权限'],
                        ]
                    ],
                    [
                        'name'          => 'system/permissions',
                        'display_name'  => '权限管理',
                        'is_menu'       => 1,
                        'children'      => [
                            ['name' => 'system/permissions/create', 'display_name' => '添加权限'],
                            ['name' => 'system/permissions/update', 'display_name' => '编辑权限'],
                            ['name' => 'system/permissions/destroy', 'display_name' => '删除权限'],
                        ]
                    ],
                ]
            ]
        ];

        foreach ($permissions as $pem1) {
            //生成一级权限
            $p1 = Permission::create([
                'name'          => $pem1['name'],
                'display_name'  => $pem1['display_name'],
                'is_menu'       => $pem1['is_menu'] ?? 0,
                'icon'          => $pem1['icon'] ?? '',
            ]);
            //为角色添加权限
            $role->givePermissionTo($p1);
            //为用户添加权限
            $user->givePermissionTo($p1);
            if (isset($pem1['children'])) {
                foreach ($pem1['children'] as $pem2) {
                    //生成二级权限
                    $p2 = Permission::create([
                        'name'          => $pem2['name'],
                        'display_name'  => $pem2['display_name'],
                        'parent_id'     => $p1->id,
                        'is_menu'       => $pem2['is_menu'] ?? 0,
                        'icon'          => $pem2['icon'] ?? '',
                    ]);
                    //为角色添加权限
                    $role->givePermissionTo($p2);
                    //为用户添加权限
                    $user->givePermissionTo($p2);
                    if (isset($pem2['children'])) {
                        foreach ($pem2['children'] as $pem3) {
                            //生成三级权限
                            $p3 = Permission::create([
                                'name'          => $pem3['name'],
                                'display_name'  => $pem3['display_name'],
                                'parent_id'     => $p2->id,
                                'is_menu'       => $pem3['is_menu'] ?? 0,
                                'icon'          => $pem3['icon'] ?? '',
                            ]);
                            //为角色添加权限
                            $role->givePermissionTo($p3);
                            //为用户添加权限
                            $user->givePermissionTo($p3);
                        }
                    }

                }
            }
        }

        //为用户添加角色
        $user->assignRole($role);
    }
}
